﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Evolutility;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Text;

public partial class MRP_Order : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["QUERY"]!=null)
        {
            string xmlFileName = @"XML/MRP_Order_MD_View.xml";
            //UIServer1.DBAllowDelete = false;
            //UIServer1.DBAllowInsert = false;
            //UIServer1.DBAllowUpdate = false;
            //UIServer1.DBAllowUpdateDetails = false;
            //UIServer1.DBAllowInsertDetails = false;
            UIServer1.DBReadOnly = true;
            this.UIServer1.XMLfile = xmlFileName;
            this.UIServer1.DBAllowSelections = true;
        }
        else if (Request.Params["type"] != null)
        {
            string xmlFileName = string.Format(@"XML/MRP_Order_MD_{0}.xml", Request.Params["type"].ToString());
            this.UIServer1.XMLfile = xmlFileName;
            this.UIServer1.Text = "样品订单";
        }
        
        
    }
    protected void UIServer1_DBBeforeUpdate(object sender, UIServer.MRPDataUpdateEventArgs e)
    {


    }
    protected void UIServer1_CmdFieldFire(object sender, UIServer.CmdFieldEventArgs e)
    {
        int id = Int32.Parse(e.PK);
        int productID = 0, quantity = 0;
        int customerID = 0;
        string sSQL = "";
        StringBuilder sbSQL = new StringBuilder();
        List<OrderMaterial> MaterialList = new List<OrderMaterial>();

        if (e.CommandText.Equals("发布"))
        {
            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");
            sSQL = string.Format("Update MRP_Order set [status]=2 where ID={0}", id);
            MRPUtility.WriteLogForSQL(sSQL);
            db.ExecuteNonQuery(CommandType.Text, sSQL);


            sSQL = string.Format("SELECT MRP_OrderDetail.ProductID,MRP_OrderDetail.Quantity,CustomerID FROM MRP_Order INNER JOIN MRP_OrderDetail ON MRP_Order.ID = MRP_OrderDetail.OrderID where MRP_Order.ID={0}", id);
            //取得MaterialID
            using (IDataReader reader = db.ExecuteReader(CommandType.Text, sSQL))
            {
                while (reader.Read())
                {
                    customerID = Int32.Parse(reader[2].ToString());
                    MaterialList.Add(new OrderMaterial(int.Parse(reader[0].ToString()), int.Parse(reader[1].ToString())));
                }

            }

            foreach (OrderMaterial Material in MaterialList)
            {
                sSQL = string.Format("Exec MRP_OrderConfirmProduct {0},{1};", Material.MaterialID, Material.Count);
                sSQL = string.Format("Exec MRP_SetPriceForOrderDetail {0},{1},{2};", Material.MaterialID, customerID,id);
                MRPUtility.WriteLogForSQL(sSQL);

                db.ExecuteNonQuery(CommandType.Text, sSQL);

            }
        }
        else if (e.CommandText.Equals("关闭订单"))
        {
            
            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");

           
            //是否要减少未交付的已分配量？需要

            string ssSQL = string.Format("Select ProductID,[RemainExportQuantity] from [View_GetRemainProductExportByOrder] where orderid={0}", id);
            
            //取得MaterialID
            using (IDataReader reader = db.ExecuteReader(CommandType.Text, ssSQL))
            {
                while (reader.Read())
                {

                    productID = int.Parse(reader["ProductID"].ToString());

                    quantity = int.Parse(reader["RemainExportQuantity"].ToString());
                    
                    //if (materialID > 0)
                    {
                        //string sSQL = string.Empty;

                        //sSQL = string.Format("SELECT MRP_OrderDetail.ProductID,MRP_OrderDetail.Quantity FROM MRP_Order INNER JOIN MRP_OrderDetail ON MRP_Order.ID = MRP_OrderDetail.OrderID where MRP_Order.ID={0}", productID);
                        sbSQL.AppendFormat("Exec MRP_OrderConfirmProduct {0},-{1};", productID, quantity);
                        sbSQL.AppendFormat("Update dbo.MRP_Stock set InLock=InLock-{1} where MRP_Stock.MaterialID = {0};", productID, quantity);
                        //db.ExecuteNonQuery(CommandType.Text, sSQL);
                    }
                }
            }
            if (productID > 0)
            {
                sbSQL.AppendFormat("Update dbo.MRP_OrderDetail set Status=5,note=note+'被用户关闭。' where OrderID = {0};", id);
                sbSQL.AppendFormat("Update dbo.MRP_Order set Status=5,Description=Description+'被用户关闭。' where ID = {0};", id);
                MRPUtility.WriteLogForSQL(sbSQL.ToString());
                db.ExecuteNonQuery(CommandType.Text, sbSQL.ToString());
            }



        }
        else if (e.CommandText.Equals("取消订单"))
        {
            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");
            string ssSQL = string.Format("Select ProductID,Quantity from MRP_OrderDetail where orderid={0}", id);
            //取得MaterialID
            using (IDataReader reader = db.ExecuteReader(CommandType.Text, ssSQL))
            {
                while (reader.Read())
                {
                    productID = int.Parse(reader[0].ToString());
                    quantity = int.Parse(reader[1].ToString());
                    //if (materialID > 0)
                    {
                        sbSQL.AppendFormat("Exec MRP_OrderConfirmProduct {0},-{1};", productID, quantity);
                        sbSQL.AppendFormat("Update dbo.MRP_Stock set InLock=InLock-{1} where MRP_Stock.MaterialID = {0};", productID, quantity);
                        //db.ExecuteNonQuery(CommandType.Text, sSQL);
                    }
                }
            }
            if (productID > 0)
            {
                sbSQL.AppendFormat("Update dbo.MRP_OrderDetail set Status=4,note=note+'被用户取消。' where OrderID = {0};", id);
                sbSQL.AppendFormat("Update dbo.MRP_Order set Status=4,Description=Description+'被用户取消。' where ID = {0}", id);
                MRPUtility.WriteLogForSQL(sbSQL.ToString());
                db.ExecuteNonQuery(CommandType.Text, sbSQL.ToString());
            }           



        }   
    }
}
